package cn.kgc.controller;

import cn.kgc.pojo.Person;
import cn.kgc.service.PersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @author Rock
 * @create 2020-08-01 10:44
 */
@Controller
@RequestMapping("/person")
public class PersonController {
    @Autowired
    private PersonService service;

    @RequestMapping(value = "/toList",method = RequestMethod.GET)
    public String toList(Model model){
        List<Person> list = service.queryAll();
        System.out.println(list.toString());
        model.addAttribute("list",list);
        return "person_list";
    }

    /**
     * 跳转到添加员工页面
     */
    @RequestMapping(value = "/toSave",method = RequestMethod.GET)
    public String toSave(){
        return "person_save";
    }

    @ResponseBody
    @RequestMapping(value = "/save",method = RequestMethod.POST)
    public String save(Person person){
        Integer count = service.save(person);
        if (count>0){
            return "success";
        }
        return "error";
    }

    @RequestMapping(value = "/toUpdate",method = RequestMethod.GET)
    public String toUpdate(Model model,Integer id){
        Person person = service.queryById(id);
        model.addAttribute("person",person);
        return "person_update";
    }

    @ResponseBody
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public String update(Person person){
        Integer count = service.updatePerson(person);
        if (count>0){
            return "success";
        }else {
            return "error";
        }
    }

    @ResponseBody
    @GetMapping("/delete")
    public String delete(Integer id){
        Integer count = service.delete(id);
        if (count>0){
            return "success";
        }else {
            return "error";
        }
    }
}
